#-*- coding:utf-8 -*-

from flask import *
from models import dao
from models import admins

home_page = Blueprint('home_page', __name__)

def makeIdBeStr(cards):
    for card in cards:
        card["id"]= str(card["_id"])
        del card["_id"]
    return cards

@home_page.route('/')
def home():
    return render_template('home/home.html')

#板
@home_page.route('/board',methods=["GET"])
def board_getall():
    isrequestall = request.args.get('get', None)
    if isrequestall == "all":
        #获取所有board
        bds = dao.get_all_boards()
        return json.dumps(bds)

@home_page.route('/board/<board_name>',methods=["GET"])
def board(board_name):
    isrequestall = request.args.get('get', None)
    if board_name is None:return
    if isrequestall == "all":
        lists = dao.get_lists_from_boards_name(board_name)
        allcards = {
            item:makeIdBeStr(dao.get_cards_from_lists_name(board_name,item))
            for item in lists}
        allcards['_lists'] = lists
        return json.dumps(allcards)
    else:
        return render_template('home/board.html',board_name=board_name)

@home_page.route("/board/create",methods=['POST'])
def board_create():
    #添加一个板
    board_name = request.form["board_name"]
    if board_name != None:
        isok = dao.add_board(board_name)
        return jsonify(is_create_ok=1,new_board_name=isok.name)
    else:
        return jsonify(is_create_ok=0,new_board_name=board_name)

@home_page.route("/board/delete/<board_name>",methods=["DELETE"])
def board_delete(board_name):
    #删除一个板
    if board_name != None:
        isok = dao.del_board(board_name)
        if isok:
            return jsonify(is_delete_ok=1,delete_board_name=board_name)
        else:
            return jsonify(is_delete_ok=0,delete_board_name=board_name)
        
@home_page.route("/board/rename/<board_name>",methods=["PUT"])
def board_rename(board_name):
    #重命名一个版
    if board_name != None:
        new_board_name = request.form['new_board_name']
        isok = dao.rename_board(board_name,new_board_name)
        if isok:
            return jsonify(is_rename_ok=1,old_board_name=board_name,new_board_name=new_board_name)
        else:
            return jsonify(is_rename_ok=0,old_board_name=board_name,new_board_name=new_board_name)

#列
@home_page.route('/list/<board_name>/<list_name>',methods=["GET"])
def list_all(board_name,list_name):
    if list_name == "all":
        #获取所有列
        return json.dumps(dao.get_lists_from_boards_name(board_name))
    else:
        return jsonify(msg="功能还在开发中")

@home_page.route('/list/create/<board_name>',methods=["POST"])
def list_create(board_name):
    #创建列
    list_name = request.form["list_name"]
    if list_name != None :
        isok = dao.add_list(board_name,list_name)
        if isok:
            return jsonify(is_create_ok=1)
        else:
            return jsonify(is_create_ok=0)

@home_page.route('/list/delete/<board_name>',methods=["POST"])
def lists_delete(board_name):
    #删除多个列
    lists_name = json.loads(request.form["lists_name"])
    if lists_name:
        isok = True
        for list_name in lists_name:
            isok = isok and dao.del_list(board_name,list_name)
        if isok:
            return jsonify(is_delete_ok=1)
        else:
            return jsonify(is_delete_ok=0)

@home_page.route('/list/delete/<board_name>/<list_name>',methods=["DELETE"])
def list_delete(board_name,list_name):
    #删除列
    if list_name != None:
        isok = dao.del_list(board_name,list_name)
        if isok:
            return jsonify(is_delete_ok=1,new_list_name=lname)
        else:
            return jsonify(is_delete_ok=0,delete_list_name=lname)

@home_page.route('/list/rename/<board_name>/<list_name>',methods=["PUT"])
def list_rename(board_name,list_name):
    #重命名列
    if list_name != None:
        new_list_name = request.form['new_list_name']
        isok = dao.rename_list(board_name,list_name,new_list_name)
        if isok:
            return jsonify(is_rename_ok=1,old_list_name=list_name,new_list_name=new_list_name)
        else:
            return jsonify(is_rename_ok=0,old_list_name=list_name,new_list_name=new_list_name)

#卡
@home_page.route('/card/<board_name>/<list_name>/<card_id>',methods=["GET"])
def card(board_name,list_name,card_id):
    if card_id == "all":
        #获取所有卡
        return json.dumps(dao.get_cards_from_lists_name(board_name,list_name))
    else:
        return jsonify(msg="功能还在开发中")

@home_page.route('/card/<board_name>/<list_name>',methods=["POST"])
def card_create(board_name,list_name):
    #创建卡
    card = request.json
    if card:
        card_id = dao.add_card(board_name,list_name,card['card_content'])
        if card_id != None:
            return jsonify(is_create_ok=1,new_card_id=card_id)
        else:
            return jsonify(is_create_ok=0,new_card_id=0)

@home_page.route('/card/<board_name>/<list_name>/<card_id>',methods=["DELETE"])
def card_delete(board_name,list_name,card_id):
    #删除卡
    if card_id != None:
        is_delete_ok = 1 if dao.del_card(board_name,list_name,card_id) else 0
        return jsonify(is_delete_ok=is_delete_ok)

@home_page.route('/card/<board_name>/<list_name>/<card_id>',methods=["PUT"])
def card_recontent(board_name,list_name,card_id):
    #重命名列
    new_content = request.json
    if new_content:
        is_recontent_ok = 1 if dao.recontent_card(
            board_name,list_name,card_id,
            new_content['card_content']) else 0
        return jsonify(is_recontent_ok=is_recontent_ok)

#会员
@home_page.route("/login",methods=["POST"])
def login():
    email = request.form['email']
    pwd = ["org",request.form['pwd']]
    isok = admins.login(email,pwd)
    if isok != False:
        return jsonify(is_a_user=1)
    else:
        return jsonify(is_a_user=0)
